import 'package:flutter_admin/models/odoo/stock_materialinout.dart';
import 'package:flutter_admin/pages/common/cry/cry.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/cry_buttons.dart';
import 'package:flutter_admin/pages/common/cry/cry_dialog.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:direct_link/direct_link.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/api/odoo/stock_picking_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/stock_picking.dart';
import 'package:flutter_admin/models/odoo/stock_production.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/utils/adaptive_util.dart';
import 'package:flutter_admin/utils/eventbus.dart';

class MaterialWarehousingList extends StatefulWidget {

  List<StockMaterialinout> stockMaterialinoutList;

  MaterialWarehousingList({required this.stockMaterialinoutList, Key? key}) : super(key: key);
  @override
  _MaterialWarehousingListState createState() =>
      _MaterialWarehousingListState();
}

class _MaterialWarehousingListState extends State<MaterialWarehousingList> {
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  PageModel page = PageModel(orders: [
    OrderItemModel(column: 'create_date', asc: false),
  ]);
  // StockProduction stockProduction = StockProduction();
  // StockPicking stockPicking = StockPicking();
  List<StockMaterialinout> stockMaterialinoutList = [];
  @override
  void initState() {
    this.stockMaterialinoutList = widget.stockMaterialinoutList;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var buttonBar = CryButtonBar(
      children: [
        CryButton(
          label: '确认',
          // padding: EdgeInsets.only(left: 10),
          iconData: Icons.save_outlined,
          onPressed: () {
            List idList = stockMaterialinoutList.map((v) {
              return int.parse(v.id!);
            }).toList();
            StockPickingOdooApi.workshopMaterialSave({
              "domainList":[["id",idList],["location_dest_id","=",int.parse(stockMaterialinoutList[0].locationDestId!)]]
            }).then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              // if(res.data["code"] != "200"){
              //   CryUtils.message(res.data["message"]);
              //   return ;
              // }
              Navigator.pop(context, true);
              EventBus.getPublisher().add({"type": "form:saved"});
              CryUtils.message(S.of(context).saved);
            });
          },
        ),
        CryButton(
          label: '取消',
          // padding: EdgeInsets.only(left: 10),
          iconData: Icons.cancel,
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ],
    );
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '目标库房',
            width: 300,
            enable: false,
            value: stockMaterialinoutList[0].locationDestName,
          ),
          Padding(
            padding: EdgeInsets.only(top: 15),
            child: SizedBox(child: buttonBar),
          ),
        ],
      ),
    );
    var table = DataTable(
      columns: [
        DataColumn(
          label: Text('入库单号'),
        ),
        DataColumn(
          label: Text('物料编码'),
        ),
        DataColumn(
          label: Text('物料名称'),
        ),
        DataColumn(
          label: Text('规格型号'),
        ),
        DataColumn(
          label: Text('数量'),
        ),
        DataColumn(
          label: Text('计量单位'),
        ),
      ],
      rows: this.stockMaterialinoutList.map((stockMaterialinout) {
        var result = DataRow(cells: [
          DataCell(Text(stockMaterialinout.documentcode ?? '--')),
          DataCell(Text(stockMaterialinout.materialcode ?? '--')),
          DataCell(Text(stockMaterialinout.materialname ?? '--')),
          DataCell(Text(stockMaterialinout.model ?? '--')),
          DataCell(Text(stockMaterialinout.quantity ?? '--')),
          DataCell(Text(stockMaterialinout.purchaseunit ?? '--')),
        ]);
        return result;
      }).toList(),
    );
    var result = Scaffold(
      key: scaffoldStateKey,
      appBar: AppBar(
        leading: Builder(
          builder: (BuildContext context) {
            final ScaffoldState? scaffold = Scaffold.maybeOf(context);
            final ModalRoute<Object?>? parentRoute = ModalRoute.of(context);
            final bool canPop = parentRoute?.canPop ?? false;

            if (canPop) {
              return BackButton();
            } else {
              return SizedBox.shrink();
            }
          },
        ),
        title: Text('原料入库确认'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 10),
          isDisplayDesktop(context) ? form : Container(),
          Expanded(child: SingleChildScrollView(child: table)),
        ],
      ),
    );
    return result;
  }

}
